import type { Node, NodeType } from "@rivet/model";
import type { PatchState } from "@rivet/state";

import { getNodeType } from "./getNodeType.ts";

//获取节点的属性
export function getNodeAttributes(
  state: PatchState,
  typeOrName: string | NodeType,
): Record<string, any> {
  const type = getNodeType(typeOrName, state.schema);
  const nodes: Node[] = [];
  state.doc.forEach((node) => {
    nodes.push(node);
  });

  const node = nodes
    .reverse()
    .find((nodeItem) => nodeItem.type.name === type.name);

  if (!node) {
    return {};
  }

  return { ...node.attrs };
}
